package org.packtracker.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import org.packtracker.client.Constants;
import org.packtracker.server.model.PersistentPackage;

public class Parser {

	public PersistentPackage parseTracker(PersistentPackage newPackage) {

		String status = Constants.UNKNOWN_STATUS;
		try {
			String trackerURL;
			if (newPackage.getTracker().equals(Constants.HONGKONGPOST)) {
				trackerURL = Constants.HONGKONGPOST_URL;
			} else if (newPackage.getTracker().equals(Constants.POSTEN)) {
				trackerURL = Constants.POSTEN_URL;
			} else {
				trackerURL = Constants.TESTTRACKER_URL;
			}

			URL url = new URL(trackerURL.replace("TRACKINGID", newPackage.getTrackingId()));

			newPackage.setTrackerUrl(url.toString());

			BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
			String line;
			String page = "";

			while ((line = reader.readLine()) != null) {
				page = page + "\n" + line;
			}
			reader.close();

			if (newPackage.getTracker().equals(Constants.HONGKONGPOST)) {
				page = page.substring(0, page.indexOf("#EndTTSEnquiry"));
				page = page.substring(page.lastIndexOf("<tr>") + 8, page.lastIndexOf("</tr>") - 8);
				page = removeHtmlTags(page).trim();
				if (!page.equals("")) {
					status = page;
				}
			} else if (newPackage.getTracker().equals(Constants.POSTEN)) {
				page = page.substring(page.indexOf("<div class=\"sporing-sendingandkolli-latestevent-text\">"), page.length());
				page = page.substring(0, page.indexOf("</strong>"));
				page = removeHtmlTags(page).trim();
				if (!page.equals("")) {
					status = page;
				}
			} else {
				page = page.substring(0, page.indexOf("#EndTTSEnquiry"));
				page = page.substring(page.lastIndexOf("<tr>") + 8, page.lastIndexOf("</tr>") - 8);
				page = removeHtmlTags(page).trim();
				if (!page.equals("")) {
					status = page;
				}
			}

		} catch (MalformedURLException malformedURLException) {
			System.out.println(malformedURLException);
		} catch (IOException ioException) {
			System.out.println(ioException);
		} catch (StringIndexOutOfBoundsException stringIndexOutOfBoundsException) {
			System.out.println(stringIndexOutOfBoundsException);
		} finally {
			newPackage.setLastStatus(status);
		}

		return newPackage;
	}

	private String removeHtmlTags(String inputString) throws StringIndexOutOfBoundsException {

		inputString = inputString.replace("\n", " ").replace("\t", " ");

		while (inputString.indexOf("<") != -1 && inputString.indexOf(">") != -1 && inputString.indexOf("<") < inputString.indexOf(">")) {

			inputString = inputString.substring(0, inputString.indexOf("<")) + inputString.substring(inputString.indexOf(">") + 1, inputString.length());
		}

		while (inputString.indexOf("  ") != -1) {
			inputString = inputString.replace("  ", " ");
		}

		return inputString;
	}
}
